package org.rain.core.codegen.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.rain.core.DownloadMapper;
import org.rain.core.codegen.service.ICodeGenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * created by yangtong on 2025/4/6 下午5:31
 * <p>
 * 代码生成器接口
 */
@RestController
@RequestMapping("/sys/codegen")
@AllArgsConstructor
@Tag(name = "代码生成器")
public class CodeGenController {

    @Autowired
    private ICodeGenService codeGenService;

    @Operation(summary = "根据表名称生成代码", description = "根据表名称生成代码")
    @DownloadMapper("/genByTable")
    public void genByTable(@RequestParam String basePath, @RequestParam String module, @RequestParam String tableName, HttpServletResponse response) throws IOException {
        byte[] data = codeGenService.gen(basePath, module, null, tableName);
        //设置响应头
        response.setHeader("Content-Disposition", "attachment; filename=\"" + tableName + ".zip\"");
        //文件类型
        response.setContentType("application/zip");
        response.getOutputStream().write(data);
        response.flushBuffer();
    }
}
